package com.ebeijia.service.user.impl;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ebeijia.mybatis.mapper.ApplyDetailInfoMapper;
import com.ebeijia.mybatis.mapper.ApplyInfoMapper;
import com.ebeijia.mybatis.pojo.ApplyDetailInfo;
import com.ebeijia.mybatis.pojo.ApplyInfo;
import com.ebeijia.service.user.ApplyDetailInfoService;
@Service("applyDetailInfoService")
public class ApplyDetailInfoServiceImpl implements ApplyDetailInfoService{
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ApplyDetailInfoMapper applyDetailInfoMapper;
	
	@Autowired
	private ApplyInfoMapper applyInfoMapper;
	
	public int insert(ApplyDetailInfo applyDetail){
		
		return applyDetailInfoMapper.insert(applyDetail);
	}
	
	@Transactional
	public int insertApplyInfo(ApplyInfo applyInfo, ApplyDetailInfo applyDetailInfo){
		int countDetailInfo = applyDetailInfoMapper.insert(applyDetailInfo);
		int countInfo = applyInfoMapper.insert(applyInfo);
		return countInfo;
	}
	
	public String copyFile(File file, String path, String name){
		InputStream in = null;
		OutputStream out = null;
		try {
			String fileName = file.getName();
			String filePath = path + name + 
				fileName.substring(fileName.lastIndexOf("."), fileName.length());
			File fileTemp = new File(filePath);
			in = new BufferedInputStream(new FileInputStream(file));
			out = new BufferedOutputStream(new FileOutputStream(fileTemp));
			byte[] b = new byte[2048];
			int i;
			while((i = in.read(b)) != -1){
				out.write(b, 0, i);
			}
			return filePath;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		} finally{
			if(in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return "";
	}

}
